﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class ItemControl : MonoBehaviour
{
    public Text labText;
    public Text playerText;
    public GameObject leftButton;
    public GameObject rightButton;
    public Image textBG;
    public Image playerTextBG;

    public TextInfo _textInfo;
    private Text _leftText;
    private Text _rightText;

    void Start()
    {
        labText = labText.GetComponent<Text>();
        playerText = playerText.GetComponent<Text>();
        TextSuit();
    }

    public void setItem(TextInfo tI)
    {
        _textInfo = tI;

        _leftText = leftButton.GetComponentInChildren<Text>();
        _rightText = rightButton.GetComponentInChildren<Text>();

        if (_textInfo.itemType == 1)
        {
            textBG.gameObject.SetActive(true);
            playerTextBG.gameObject.SetActive(false);
            labText.gameObject.SetActive(true);
            playerText.gameObject.SetActive(false);
            leftButton.SetActive(false);
            rightButton.SetActive(false);
            labText.text = _textInfo.system;
            //labText.text = labText.text.Replace("\\n", "\n");
            //设置生命值
            GameObject pi = GameObject.Find("PlayerInfo");
            pi.SendMessage("SetHealth", _textInfo.health1);
        }
        else
        {
            textBG.gameObject.SetActive(false);
            labText.gameObject.SetActive(false);
            playerText.gameObject.SetActive(false);
            leftButton.SetActive(true);
            rightButton.SetActive(true);
            _leftText.text = _textInfo.text1;
            //_leftText.text = _leftText.text.Replace("\\n", "\n");
            _rightText.text = _textInfo.text2;
            //_rightText.text = _rightText.text.Replace("\\n", "\n");
            ScrollRectControl.Instance.isStop = true;
        }

    }
    //选择左边
    public void LeftButton()
    {
        //生成玩家选择的文字
        GameObject go = transform.Find("Text_Player").gameObject;
        go.SetActive(true);
        leftButton.SetActive(false);
        rightButton.SetActive(false);
        Text t = go.GetComponent<Text>();
        string[] arr = _textInfo.system.Split("$"[0]);
        if (arr.Length == 2)
        {
            t.text = arr[0];
        }
        else
        {
            t.text = _textInfo.text1;
        }
        //t.text = t.text.Replace("\\n", "\n");

        //设置背景
        GameObject bg = transform.Find("Image").gameObject;
        bg.SetActive(true);

        //记录选择的KEY
        string chooseKey = "playerChoose" + _textInfo.id.ToString();
        PlayerPrefs.DeleteKey(chooseKey);
        PlayerPrefs.SetInt(chooseKey, 1);

        //设置生命值
        GameObject pi = GameObject.Find("PlayerInfo");
        pi.SendMessage("SetHealth", _textInfo.health1);

        //游戏继续
        ScrollRectControl.Instance.gameFlow = _textInfo.nextid1;
        ScrollRectControl.Instance.isStop = false;
        //print("生命值: " + _textInfo.health1);
    }
    //选择右边
    public void RightButton()
    {
        //生成玩家选择的文字
        GameObject go = transform.Find("Text_Player").gameObject;
        go.SetActive(true);
        leftButton.SetActive(false);
        rightButton.SetActive(false);
        Text t = go.GetComponent<Text>();
        string[] arr = _textInfo.system.Split("$"[0]);
        if (arr.Length == 2)
        {
            t.text = arr[1];
        }
        else
        {
            t.text = _textInfo.text2;
        }
        //t.text = t.text.Replace("\\n", "\n");

        //设置背景
        GameObject bg = transform.Find("Image").gameObject;
        bg.SetActive(true);

        //记录选择的KEY
        string chooseKey = "playerChoose" + _textInfo.id.ToString();
        PlayerPrefs.DeleteKey(chooseKey);
        PlayerPrefs.SetInt(chooseKey, 2);

        //设置生命值
        GameObject pi = GameObject.Find("PlayerInfo");
        pi.SendMessage("SetHealth", _textInfo.health2);

        //游戏继续
        ScrollRectControl.Instance.gameFlow = _textInfo.nextid2;
        ScrollRectControl.Instance.isStop = false;
        //print("生命值: " + _textInfo.health2);
    }


    //快速生成才使用该方法
    public void LeftChoose()
    {
        //生成玩家选择的文字
        GameObject go = transform.Find("Text_Player").gameObject;
        go.SetActive(true);
        leftButton.SetActive(false);
        rightButton.SetActive(false);
        Text t = go.GetComponent<Text>();
        string[] arr = _textInfo.system.Split("$"[0]);
        if (arr.Length == 2)
        {
            t.text = arr[0];
        }
        else
        {
            t.text = _textInfo.system;
        }

        //t.text = t.text.Replace("\\n", "\n");

        //设置背景
        GameObject bg = transform.Find("Image").gameObject;
        bg.SetActive(true);

        //设置生命值
        GameObject pi = GameObject.Find("PlayerInfo");
        pi.SendMessage("SetHealth", _textInfo.health1);
        //print("生命值: " + _textInfo.health1);
    }

    //快速生成才使用该方法
    public void RightChoose()
    {
        //生成玩家选择的文字
        GameObject go = transform.Find("Text_Player").gameObject;
        go.SetActive(true);
        leftButton.SetActive(false);
        rightButton.SetActive(false);
        Text t = go.GetComponent<Text>();
        string[] arr = _textInfo.system.Split("$"[0]);
        if (arr.Length == 2)
        {
            t.text = arr[1];
        }
        else
        {
            t.text = _textInfo.system;
        }
        //t.text = t.text.Replace("\\n", "\n");

        //设置背景
        GameObject bg = transform.Find("Image").gameObject;
        bg.SetActive(true);

        //设置生命值
        GameObject pi = GameObject.Find("PlayerInfo");
        pi.SendMessage("SetHealth", _textInfo.health2);
        //print("生命值: " + _textInfo.health2);
    }

    //自适应屏幕大小
    void TextSuit()
    {
        //屏幕的宽度
        float textWidth = Screen.width * 0.7f;

        //文字的自适应
        RectTransform textRC = labText.GetComponent<RectTransform>();
        RectTransform textRCp = playerText.GetComponent<RectTransform>();
        textRC.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 30, textWidth);
        textRCp.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, 30, textWidth);

        //按钮的自适应
        RectTransform leftRC = leftButton.GetComponent<RectTransform>();
        RectTransform rightRC = rightButton.GetComponent<RectTransform>();
        leftRC.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, Screen.width * 0.01f, Screen.width * 0.48f);
        rightRC.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, Screen.width * 0.01f, Screen.width * 0.48f);

        //背景自适应
        RectTransform imageBG = textBG.GetComponent<RectTransform>();
        RectTransform playerImageBG = playerTextBG.GetComponent<RectTransform>();
        imageBG.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 0, textWidth + 40);
        playerImageBG.SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, 0, textWidth + 40);
    }

    public void DestroyGO()
    {
        Destroy(this.gameObject);
    }
}
